package com.nhutcm.trackingthief.utils.sendemail;

import java.io.File;
import java.util.Calendar;

import com.nhutcm.trackingthief.utils.Constant;

import android.os.AsyncTask;
import android.util.Log;

/**
 * An AsyncTask for send mail
 * 
 * @author Chau Minh Nhut (nhutcm@viettel.com.vn)
 * 
 */

public class SendMailTask extends AsyncTask<String, Void, String> {

	public static final String TAG = "com.nhutcm.trackingthief.utils.sendmail.SendMailTask";

	@Override
	protected String doInBackground(String... params) {
		GMailSender sender = new GMailSender(Constant.MAIL_FROM,
				Constant.MAIL_FROM_PASSWORD);
		File file = null;
		try {
			String filePath = params[0];
			String email = params[1];

			file = new File(filePath);
			if (file.exists() && file.isFile()) {
				Calendar c = Calendar.getInstance();
				String strTime = String.valueOf(c.getTime());

				sender.sendMail(strTime, Constant.lastestAddress,
						Constant.MAIL_FROM, email, file);
			} else {
				if (Constant.lastestAddress.equals("") || filePath.equals("")) {
				} else {
					sender.sendMail(Constant.lastestAddress,
							Constant.lastestAddress, Constant.MAIL_FROM, email);
				}
			}

		} catch (Exception e) {
			Log.e(TAG, e.getMessage(), e);
			return "Email Not Sent";
		}
		return file.getPath();
	}

	@Override
	protected void onPostExecute(String result) {
	}

	@Override
	protected void onPreExecute() {
	}

	@Override
	protected void onProgressUpdate(Void... values) {
	}
}